<template>
  <div class="search">
    <Card>
      <Form
        ref="searchForm"
        :model="searchForm"
        inline
        :label-width="70"
        class="search-form"
      >
        <Form-item label="时间" prop="createTime">
          <DatePicker
            v-model="selectDate"
            type="daterange"
            format="yyyy-MM-dd HH:mm:ss"
            clearable
            @on-change="selectDateRange"
            placeholder="选择起始时间"
            style="width: 200px"
          >
          </DatePicker>
        </Form-item>
        <Form-item label="手机号" prop="mobile">
          <Input
            type="text"
            v-model="searchForm.mobile"
            placeholder="请输入手机号"
            clearable
            style="width: 200px"
          />
        </Form-item>
        <Form-item label="区域" prop="region">
          <region
            style="width: 350px"
            ref="region"
            @selected="selectedRegionSearch"
          />
        </Form-item>
        <Button
          @click="handleSearch"
          type="primary"
          icon="ios-search"
          class="search-btn"
          >搜索</Button
        >
        <Button class="search-btn" type="primary" @click="handleReset"
          >重置</Button
        >
      </Form>
      <Row class="operation padding-row">
        <Button @click="all" type="primary">全部</Button>
        <Button @click="treat" type="primary">待审核</Button>
        <Button @click="already" type="primary">已审核</Button>
      </Row>
      <Table
        :loading="loading"
        border
        :columns="columns"
        :data="data"
        ref="table"
      ></Table>
      <Row type="flex" justify="end" class="mt_10">
        <Page
          :current="searchForm.pageNumber"
          :total="total"
          :page-size="searchForm.pageSize"
          @on-change="changePage"
          @on-page-size-change="changePageSize"
          :page-size-opts="[10, 20, 50, 100]"
          size="small"
          show-total
          show-elevator
          show-sizer
        ></Page>
      </Row>
    </Card>
    <!-- 审核模态框 -->
    <Modal
      :title="modalTitle"
      v-model="modalVisible"
      :mask-closable="false"
      :width="500"
    >
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate">
        <FormItem label="审核意见" prop="auditStatus">
          <Select clearable style="width: 300px" @on-change="examineChange">
            <Option value="2">审核通过</Option>
            <Option value="3">驳回</Option>
          </Select>
        </FormItem>
        <FormItem label="备注" prop="auditMsg">
          <Input
            v-model="form.auditMsg"
            maxlength="20"
            clearable
            style="width: 300px"
          />
        </FormItem>
      </Form>
      <p style="text-align: center; color: #999">
        解除该合伙人下辖会员且回收推广钱包余额
      </p>
      <div slot="footer" style="text-align: center">
        <Button
          type="primary"
          :loading="submitLoading"
          @click="handleSubmitExamine"
          >提交
        </Button>
      </div>
    </Modal>

    <!-- 退回金额模态框 -->
    <Modal
      :title="modalTitle"
      v-model="backmoney"
      :mask-closable="false"
      :width="500"
    >
      <Form
        ref="addMemberForm"
        :model="addMemberForm"
        :label-width="100"
        :rules="applyRule"
      >
        <FormItem label="现金额（元）:" prop="backAmount">
          <Input
            v-if="modalTitle == '退回金额'"
            v-model="addMemberForm.backAmount"
            maxlength="20"
            placeholder="请输入退回现金额"
            clearable
            style="width: 100%"
          />
          <span v-if="modalTitle == '查看退回金额'">{{
            addMemberForm.backAmount
          }}</span>
        </FormItem>
        <FormItem label="收款人:" prop="backUser">
          <Input
            v-if="modalTitle == '退回金额'"
            v-model="addMemberForm.backUser"
            maxlength="20"
            placeholder="请输入姓名"
            clearable
            style="width: 100%"
          />
          <span v-if="modalTitle == '查看退回金额'">{{
            addMemberForm.backUser
          }}</span>
        </FormItem>
        <FormItem label="收款人手机:" prop="backMobile">
          <Input
            v-if="modalTitle == '退回金额'"
            v-model="addMemberForm.backMobile"
            maxlength="20"
            placeholder="请输入手机号"
            clearable
            style="width: 100%"
          />
          <span v-if="modalTitle == '查看退回金额'">{{
            addMemberForm.backMobile
          }}</span>
        </FormItem>
      </Form>
      <p
        style="text-align: center; color: #999"
        v-if="modalTitle == '退回金额'"
      >
        线下与合伙人结算清账后在此登记
      </p>
      <div slot="footer">
        <Button
          type="text"
          v-if="modalTitle == '退回金额'"
          @click="recommendCancel"
          >取消</Button
        >
        <Button
          type="primary"
          v-if="modalTitle == '退回金额'"
          :loading="submitLoading"
          @click="handleSubmitRefund"
          >保存</Button
        >
      </div>
    </Modal>

    <!-- 已解绑模态框 -->
    <Modal
      :title="modalTitle"
      v-model="nameList"
      :mask-closable="false"
      :width="700"
    >
      <Form ref="reject" :model="reject" :label-width="100">
        <Table
          :loading="unbindLoading"
          border
          :columns="column"
          :data="nbindData"
          ref="table"
        ></Table>
        <Row type="flex" justify="end" class="mt_10">
          <Page
            :current="reject.pageNumber"
            :total="nbindTotal"
            :page-size="reject.pageSize"
            @on-change="changePage"
            @on-page-size-change="changePageSize"
            :page-size-opts="[10, 20, 50, 100]"
            size="small"
            show-total
            show-elevator
            show-sizer
          ></Page>
        </Row>
      </Form>
      <div slot="footer" style="display: none">
        <Button type="text" @click="recommendCancel">取消</Button>
        <!-- <Button type="primary" :loading="submitLoading" @click="handleSubmit">保存</Button> -->
      </div>
    </Modal>
  </div>
</template>

<script>
import * as API_Member from "@/api/member.js";
import region from "../seller/shop/region.vue";
import { getShopListData } from "@/api/shops";
export default {
  components: {
    region,
  },
  data() {
    return {
      modalVisible: false, //审核模态框
      backmoney: false, //返回金额模态框
      nameList: false, //已解绑模态框
      modalTitle: "", //添加售后原因弹出框标题
      submitLoading: false, // 添加或编辑提交状态
      // 审核意见提交表单
      form: {
        auditId: "",
        auditStatus: "",
        auditMsg: "",
      }, //添加编辑表单
      formValidate: {
        opinion: [
          {
            required: true,
            message: "请输入售后原因",
            trigger: "blur",
          },
        ],
      },
      // 退回金额表单
      addMemberForm: {
        auditId: "",
        backAmount: "",
        backUser: "",
        backMobile: "",
      },
      applyRule: {
        // 审核验证规则
        mobilephone: [
          {
            message: "请输入手机号",
          },
        ],
        money: [
          {
            message: "请输入退回现金额",
          },
        ],
        people: [
          {
            message: "请输入姓名",
          },
        ],
      },
      //查看解绑名单或查看下级名单表单
      reject: {
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      selectDate: null, // 选择一个时间段
      // 搜索框表单
      searchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        // sort: "createTime", // 默认排序字段
        order: "desc", // 默认排序方式
        createTime: "",
        startTime: "", // 起始时间
        endTime: "", // 终止时间
        auditStatus: "", //状态 0已审核  1待审核 2审核通过 3审核驳回  4撤回
        mobile: "", // 手机号
        region: "", // 区域
      },
      loading: false, // 表单加载状态
      unbindLoading: false, // 解绑名单加载状态
      total: 0, // 表单数据总数
      data: [], // 表单数据
      nbindData: [], // 已解绑名单表单数据
      nbindTotal: 0, //已解绑名单数据总数
      column: [
        // 解绑名单表头
        {
          title: "序号",
          key: "",
          align: "center",
          width: 80,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.reject.pageNumber - 1) * this.reject.pageSize +
                1
            );
          },
        },
        {
          title: "手机号",
          key: "mobile",
          align: "center",
          width: 120,
        },
        {
          title: "昵称 ",
          key: "nickName",
          align: "center",
        },
        {
          title: "级别",
          key: "levelName",
          align: "center",
        },
        {
          title: "地区",
          key: "region",
          align: "center",
        },
      ],
      columns: [
        // 表头
        {
          title: "序号",
          key: "",
          align: "center",
          width: 80,
          render: (h, params) => {
            return h(
              "span",
              params.index +
                (this.searchForm.pageNumber - 1) * this.searchForm.pageSize +
                1
            );
          },
        },
        {
          title: "提交时间",
          key: "createTime",
          align: "center",
		  width: 200,
        },
        {
          title: "手机号",
          key: "mobile",
          align: "center",
		  width: 120,
        },
        {
          title: "姓名",
          key: "realName",
          align: "center",
		  width: 120,
        },
        {
          title: "身份证号",
          key: "idCardNumber",
          align: "center",
		   width: 200,
        },
        {
          title: "用户昵称",
          key: "nickName",
          align: "center",
		  width: 120,
        },
        {
          title: "区域",
          key: "region",
          align: "center",
		   width: 300,
        },
        {
          title: "用户类型",
          key: "partnerName",
          align: "center",
          width: 100,
        },
        {
          title: "注销原因",
          key: "revokeMsg",
          align: "center",
		  width: 120,
        },
        {
          title: "已缴端口费",
          key: "rechargeMoney",
          align: "center",
		  width: 80,
        },
        {
          title: "充值钱余额",
          key: "recharge",
          align: "center",
		  width: 100,
        },
        {
          title: "推广钱包余额",
          key: "promote",
          align: "center",
		  width: 100,
        },
        {
          title: "下辖会员数",
          key: "memberNum",
          align: "center",
          width: 100,
          tooltip: true,
        },
        {
          title: "状态",
          key: "auditStatus",
          align: "center",
          width: 180,
          tooltip: true,
          render: (h, params) => {
            if (params.row.auditStatus == "1") {
              return h("Tag", { props: { color: "blue" } }, "待审核");
            } else if (params.row.auditStatus == "2") {
              return h("Tag", { props: { color: "geekblue" } }, "审核通过");
            } else if (params.row.auditStatus == "3") {
              return h("Tag", { props: { color: "red" } }, "审核驳回");
            } else {
              return h("Tag", { props: { color: "green" } }, "已撤回");
            }
          },
        },
        {
          title: "操作",
          key: "action",
          tooltip: true,
          width: 200,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            let enableOrDisable = "";
            if (params.row.auditStatus == "1") {
              enableOrDisable = h(
                "Button",
                {
                  props: {
                    type: "success",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.examine(params.row);
                    },
                  },
                },
                "审核"
              );
            } else if (params.row.auditStatus == "2") {
              enableOrDisable = h(
                "Button",
                {
                  props: {
                    size: "small",
                    type: "warning",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.unbind(params.row);
                    },
                  },
                },
                "已解绑"
              );
            }
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    size: "small",
                    type: "success",
                  },
                  style: {
                    marginRight: "5px",
                    display:
                      params.row.auditStatus == "1" ? "inline-block" : "none",
                  },
                  on: {
                    click: () => {
                      this.unbind(params.row);
                    },
                  },
                },
                "查看下级名单"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "warning",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                    display:
                      params.row.auditStatus == "2" &&
                      params.row.backAmount !== null
                        ? "inline-block"
                        : "none",
                  },
                  on: {
                    click: () => {
                      this.viewBackMoney(params.row);
                    },
                  },
                },
                "查看退回金额"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "success",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                    display:
                      params.row.auditStatus == "2" &&
                      params.row.backAmount == null
                        ? "inline-block"
                        : "none",
                  },
                  on: {
                    click: () => {
                      this.backMoney(params.row);
                    },
                  },
                },
                "退回金额"
              ),
              enableOrDisable,
            ]);
          },
        },
      ],
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取列表数据
    getDataList() {
      API_Member.getCancellationList(this.searchForm).then((res) => {
        if (res.success) {
          this.data = res.result.records;
          this.total = res.result.total;
          this.addMemberForm.backAmount = res.result.records.backAmount;
        } else {
          this.total = this.data.length;
          this.loading = false;
        }
      });
    },
    // 获取已解绑名单数据
    getUnbindDataList(memberId) {
      API_Member.getUnbindDataList({ memberId }).then((res) => {
        if (res.success) {
          this.nbindData = res.result.records;
          this.nbindTotal = res.result.total;
        }
      });
    },
    changePage(v) {
      // 改变页码
      this.searchForm.pageNumber = v;
      this.getDataList();
    },
    changePageSize(v) {
      // 改变页数
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    selectDateRange(v) {
      if (v) {
        this.searchForm.startTime = v[0];
        this.searchForm.endTime = v[1];
      }
    },
    // 选中的地址
    selectedRegionSearch(val) {
      let ids = val[0];
      this.searchForm.regionId = ids[ids.length - 1];
      // this.searchForm.regionId = val[0][val[0].length - 1];
    },
    handleSearch() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    // 审核
    examine(v) {
      this.modalVisible = true;
      this.modalTitle = "审核意见";
      this.form.auditId = v.auditId;
    },
    // 审核意见下拉选改变
    examineChange(v) {
      this.form.auditStatus = v;
    },
    // 退回金额
    backMoney(v) {
      this.backmoney = true;
      this.modalTitle = "退回金额";
      this.addMemberForm.auditId = v.auditId;
      this.addMemberForm.backAmount = v.backAmount;
      this.addMemberForm.backUser = v.username;
      this.addMemberForm.backMobile = v.mobile;
    },
    // 查看退回金额
    viewBackMoney(v) {
      this.backmoney = true;
      this.modalTitle = "查看退回金额";
      this.addMemberForm.auditId = v.auditId;
      this.addMemberForm.backAmount = v.backAmount;
      this.addMemberForm.backUser = v.username;
      this.addMemberForm.backMobile = v.mobile;
    },
    // 已解绑 查看下级名单
    unbind(v) {
      this.form.reason = v.reason;
      this.form.id = v.id;
      this.nameList = true;
      this.modalTitle = "已解绑名单";
      this.getUnbindDataList(v.memberId);
    },
    // 审核模态框提交
    handleSubmitExamine() {
      API_Member.submitExamine(this.form).then((res) => {
        if (res.success) {
          this.modalVisible = false;
          this.$Message.success("审核通过！");
          this.getDataList();
        }
      });
    },
    // 退回金额模态框提交
    handleSubmitRefund() {
      API_Member.safeReturnAmount(this.addMemberForm).then((res) => {
        if (res.success) {
          this.backmoney = false;
          this.$Message.success("提交成功！");
          this.getDataList();
        }
      });
    },
    recommendCancel() {
      this.backmoney = false;
    },
    // 重置
    handleReset() {
      this.selectDate = "";
      this.reset = true;
      (this.searchForm = {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
        // sort: "createTime", // 默认排序字段
        order: "desc", // 默认排序方式
        createTime: "",
        startTime: "", // 起始时间
        endTime: "", // 终止时间
        auditStatus: "", //状态 0已审核  1待审核 2审核通过 3审核驳回  4撤回
        mobile: "", // 手机号
        region: "", // 区域
      }),
        (this.$refs.region.addr = []);
      this.getDataList();
    },
    all() {
      this.handleReset();
    },
    treat() {
      this.searchForm.auditStatus = "1";
      this.getDataList();
    },
    already() {
      this.searchForm.auditStatus = "0";
      this.getDataList();
    },
  },
};
</script>

<style>
</style>